package com.cat.dynamicPlanning02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/integer-replacement/description/?envType=problem-list-v2&envId=dynamic-programming
 * @create 2025/9/12 14:41
 * @since JDK17
 */

public class Solution04 {
    int f(long n) {
        if (n == 1) {
            return 0;
        }
        if (n == 3) {
            return 2;
        }
        if (n % 2 == 0) {
            return 1 + f(n / 2);
        }

        if ((n + 1) / 2 % 2 == 0) {
            return 1 + f(n + 1);
        } else {
            return 1 + f(n - 1);
        }
    }


    public int integerReplacement(int n) {

        return f(n);
    }
}
